//
//  CurrenciesEntity.m
//  HYApp
//
//  Created by Jianbo Cai on 2020/8/27.
//  Copyright © 2020 Unidax. All rights reserved.
//

#import "CurrenciesEntity.h"

@implementation Currencie

+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper {
    return @{
             @"ID": @"id"
             };
}

@end



@implementation CountryEntity
@end




@interface CurrenciesEntity ()

@property (nonatomic, strong) NSDictionary *countriesDic;
@property (nonatomic, strong) NSArray *symbolSortArray;

@end

@implementation CurrenciesEntity

CurrenciesEntity *Currencies() {
    return [CurrenciesEntity standardDefaults];
}

static CurrenciesEntity *currencies_defaults = nil;
+ (CurrenciesEntity *)standardDefaults {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        currencies_defaults = [[CurrenciesEntity alloc] init];
    });
    return currencies_defaults;
}

- (Currencie *)getCurrencieWithDisplaySymbol:(NSString *)display_symbol {
    for (Currencie *entity in self.currencieList) {
        if ([entity.display_symbol isEqualToString:display_symbol]) {
            return entity;
        }
    }
    return nil;
}

- (CountryEntity *)getCountryEntityWithCountryId:(NSString *)countryId {
    CountryEntity *entity = [[CountryEntity alloc]init];
    entity.countryId = countryId;
    entity.countryName = self.countriesDic[countryId];
    return entity;
}

- (NSArray *)symbolSortArray {
    if (!_symbolSortArray) {
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:5];
        for (Currencie *currencie in Currencies().currencieList) {
            [array addObject:currencie.display_symbol];
        }
        _symbolSortArray = [NSArray arrayWithArray:array];
    }
    return _symbolSortArray;
}

- (NSArray<CountryEntity *> *)countryList {
    if (!_countryList) {
        NSMutableArray *array = [NSMutableArray array];
        for (NSString *key in self.countriesDic.allKeys) {
            CountryEntity *entity = [[CountryEntity alloc]init];
            entity.countryId = key;
            entity.countryName = self.countriesDic[key];
            [array addObject:entity];
        }
        _countryList = [NSArray arrayWithArray:array];
    }
    return _countryList;
}

- (NSDictionary *)countriesDic {
    if (!_countriesDic) {
        NSString *string = [JFInternationalHelper currentLanguage];
        NSString *resource;
        if ([string isEqualToString:@"zh-Hans"]) {
            resource = @"zh";
        }else if ([string isEqualToString:@"zh-Hant"]) {
            resource = @"zh";
        }else {
            resource = @"en";
        }
        
        NSString *path = [[NSBundle mainBundle] pathForResource:resource ofType:@"json"];
        NSData *jsonData = [[NSData alloc] initWithContentsOfFile:path];
        NSError *error;
        id jsonObj = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
        _countriesDic = jsonObj[@"countries"];
    }
    return _countriesDic;
}

@end
